package com.yh.workbill.ui.editworkbill.mvp;

import android.content.SharedPreferences;

import com.yh.workbill.App;
import com.yh.workbill.AppGlobals;
import com.yh.workbill.base.IBaseModel;
import com.yh.workbill.db.Company;
import com.yh.workbill.db.CompanyDao;
import com.yh.workbill.db.UnitCompany;
import com.yh.workbill.db.UnitCompanyDao;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;
import com.yh.workbill.model.WorkBill;
import com.yh.workbill.model.WorkDevices;
import com.yh.workbill.network.BaseCallback;
import com.yh.workbill.network.OkHttpService;
import com.yh.workbill.ui.addworkbill.mvp.AddWorkBill_Model;
import com.yh.workbill.ui.companyInfo.mvp.AddCompanyInfo_Model;
import com.yh.workbill.ui.leaseInfo.mvp.AddLease_Model;

import java.util.Calendar;
import java.util.List;

import androidx.arch.core.executor.ArchTaskExecutor;

import static com.yh.workbill.utils.Config.SP_USER;

public class EditWork_Model {

    interface EditWorkBillCallBack extends IBaseModel.Fail {
        void onSuccess(String msg);
    }

    public void getAllDevices(AddWorkBill_Model.DevicesBack callBack) {
        OkHttpService.getInstance().getDevices(new BaseCallback<List<WorkDevices>>() {

            @Override
            public void accessSuccess(int code, String msg, List<WorkDevices> data) {
                if (code == 200) {
                    callBack.Suceess(data);
                } else {
                    callBack.fail(msg);
                }
            }

            @Override
            public void accessFailure(String errorMessage) {
                callBack.fail(errorMessage);
            }
        });
    }

    public void updataWorkBill(WorkBill workBill, EditWorkBillCallBack callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            OkHttpService.getInstance().updateWorkBill(
                    workBill.uuid,
                    singleUserInfo.getUuid(),
                    workBill.work_time,
                    workBill.work_unit,
                    workBill.work_content,
                    workBill.work_car_number,
                    workBill.work_device,
                    workBill.work_use_time,
                    workBill.company_name,
                    workBill.work_time_price, new BaseCallback<String>() {
                        @Override
                        public void accessSuccess(int code, String msg, String data) {
                            ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                                if (code == 200) {
                                    callBack.onSuccess(msg);
                                } else {
                                    callBack.fail(msg);
                                }
                            });
                        }

                        @Override
                        public void accessFailure(String errorMessage) {
                            ArchTaskExecutor.getMainThreadExecutor().execute(() -> callBack.fail(errorMessage));
                        }
                    });
        });
    }

    public void delWorkBill(String uuid, EditWorkBillCallBack callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            OkHttpService.getInstance().delWorkBill(uuid, singleUserInfo.getUuid(), new BaseCallback<String>() {
                @Override
                public void accessSuccess(int code, String msg, String data) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        if (code == 200) {
                            callBack.onSuccess(msg);
                        } else {
                            callBack.fail(msg);
                        }
                    });
                }

                @Override
                public void accessFailure(String errorMessage) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> callBack.fail(errorMessage));
                }
            });
        });
    }

    public void addUnitName(String work_use_company, AddLease_Model.AddLeaseCallback callback) {
        ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
                String phone = sp.getString("phone", "");
                UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
                UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);

                UnitCompanyDao unitCompanyDao = App.appDataBase.getUnitCompanyDao();
                UnitCompany unitComany = unitCompanyDao.getUnitComany(work_use_company);
                if (unitComany == null) {
                    OkHttpService.getInstance().postAddUnitCompanyName(singleUserInfo.getUuid(), work_use_company, new BaseCallback<String>() {
                        @Override
                        public void accessSuccess(int code, String msg, String data) {
                            ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                                if (code == 200) {
                                    callback.onSuccess(work_use_company);
                                } else {
                                    callback.fail(msg);
                                }
                            });
                        }

                        @Override
                        public void accessFailure(String errorMessage) {
                            ArchTaskExecutor.getMainThreadExecutor().execute(() -> callback.fail(errorMessage));
                        }
                    });
                }
            }
        });

    }

    public void addCompanyInfo(String company_name, String company_address, String company_phone, AddCompanyInfo_Model.AddCallBack callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);

            CompanyDao companyDao = App.appDataBase.getCompanyDao();
            Company company = companyDao.getCompany(company_name);
            if (company == null) {
                OkHttpService.getInstance().postAddCompanyName(singleUserInfo.getUuid(), company_name, company_address, company_phone, new BaseCallback<String>() {
                    @Override
                    public void accessSuccess(int code, String msg, String data) {
                        ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                            if (code == 200) {
                                callBack.onSuccess(company_name, company_address, company_phone);
                            } else {
                                callBack.fail(msg);
                            }
                        });
                    }

                    @Override
                    public void accessFailure(String errorMessage) {
                        ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                            callBack.fail(errorMessage);
                        });
                    }
                });
            }
        });
    }
}
